<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['loan:loan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['loan:loan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['loan:loan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['loan:loan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="loanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="贷款申请ID" align="center" prop="loanId" />
      <el-table-column label="期数种类(单位:期)" align="center" prop="periodsName">
        <template slot-scope="scope">
          <span>{{ (scope.row.periodsName)+"期"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="利率种类(单位:%)" align="center" prop="rateName">        
        <template slot-scope="scope">
          <span>{{ (scope.row.rateName)+"%"}}</span>
        </template>
        </el-table-column>
      <el-table-column label="还款方式" align="center" prop="refundName" />
      <el-table-column label="贷款额度(单位:元)" align="center" prop="loanRealQuota">
        <template slot-scope="scope">
          <span>{{ (scope.row.loanRealQuota)+"元"}}</span>
        </template>
        </el-table-column>
      <el-table-column label="贷款时间" align="center" prop="loanTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loanTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="贷款凭证" align="center" prop="loanProof">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.loanProof"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="还款计划" align="center" prop="repayId" />
      <el-table-column label="贷款人" align="center" prop="userId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['loan:loan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['loan:loan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改贷款申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="期数种类" prop="periodsId">
          <el-select v-model="periodsId" placeholder="请选择">
            <el-option v-for="item in periods" :key="item.periodsName" :label="item.periodsName" :value="item.periodsId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="利率种类" prop="rateId">
          <el-select v-model="rateId" placeholder="请选择">
            <el-option v-for="item in rate" :key="item.rateName" :label="item.rateName" :value="item.rateId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="还款方式" prop="refundId">
          <el-select v-model="refundId" placeholder="请选择">
            <el-option v-for="item in refund" :key="item.refundName" :label="item.refundName" :value="item.refundId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="贷款额度" prop="loanRealQuota">
          <el-input v-model="form.loanRealQuota" placeholder="请输入贷款额度" />
        </el-form-item>
        <el-form-item label="贷款时间" prop="loanTime">
          <el-date-picker clearable
            v-model="form.loanTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择贷款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="贷款凭证" prop="loanProof">
          <el-upload class="avatar-uploader" action="http://localhost:9410/upload/uploadFlie" :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="还款计划" prop="repayId">
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLoan, getLoan, delLoan, addLoan, updateLoan } from "@/api/loan/loan";
import { listRate } from "@/api/loan/rate";
import { listPeriods } from "@/api/loan/periods";
import { listRefund } from "@/api/loan/refund";
export default {
  name: "Loan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbRefund: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 贷款申请表格数据
      loanList: [],
      // 还款方式表格数据
      tbRefundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // periodsId
      periodsId:'',
      periods:[],
      // rateId
      rateId: '',
      rate:[],
       // refundId
      refundId: '',
      refund:[],
      //数据凭证路径
      imageUrl: '',

    };
  },
  created() {
    this.getList();
    this.getListPeriods();
    this.getListRate();
    this.getListRefund();
  },
  methods: {
    /** 查询贷款申请列表 */
    getList() {
      this.loading = true;
      listLoan(this.queryParams).then(response => {
        this.loanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //上传借贷凭证
    handleAvatarSuccess(res) {
      // console.log("凭证",res);
        this.imageUrl = res.msg;
      },
    /** 查询期数列表 */
    getListPeriods() {
      this.loading = true;
      listPeriods(this.queryParams).then(response => {
        this.periods = response.rows;
      });
    },
    /** 查询利率列表 */
    getListRate() {
      this.loading = true;
      listRate(this.queryParams).then(response => {
        this.rate = response.rows;
      });
    },
    /** 查询还款方式列表 */
    getListRefund() {
      this.loading = true;
      listRefund(this.queryParams).then(response => {
        this.refund = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        loanId: null,
        periodsId: null,
        rateId: null,
        refundId: null,
        loanRealQuota: null,
        loanTime: null,
        loanProof: null,
        repayId: null,
        userId: null,
        status: null,
        delFlag: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null
      };
      this.tbRefundList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.loanId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加贷款申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const loanId = row.loanId || this.ids
      getLoan(loanId).then(response => {
        this.form = response.data;
        this.tbRefundList = response.data.tbRefundList;
        this.open = true;
        this.title = "修改贷款申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbRefundList = this.tbRefundList;
          if (this.form.loanId != null) {
            updateLoan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLoan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const loanIds = row.loanId || this.ids;
      this.$modal.confirm('是否确认删除贷款申请编号为"' + loanIds + '"的数据项？').then(function() {
        return delLoan(loanIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 还款方式序号 */
    rowTbRefundIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 还款方式添加按钮操作 */
    handleAddTbRefund() {
      let obj = {};
      obj.refundName = "";
      obj.status = "";
      this.tbRefundList.push(obj);
    },
    /** 还款方式删除按钮操作 */
    handleDeleteTbRefund() {
      if (this.checkedTbRefund.length == 0) {
        this.$modal.msgError("请先选择要删除的还款方式数据");
      } else {
        const tbRefundList = this.tbRefundList;
        const checkedTbRefund = this.checkedTbRefund;
        this.tbRefundList = tbRefundList.filter(function(item) {
          return checkedTbRefund.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTbRefundSelectionChange(selection) {
      this.checkedTbRefund = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('loan/loan/export', {
        ...this.queryParams
      }, `loan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>


<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
